package plus.easydo.starter.plugins.gen.configure;

import org.apache.velocity.runtime.resource.loader.DataSourceResourceLoader;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import plus.easydo.jdbc.annotation.EnableJdbcDynamicDataSource;
import plus.easydo.jdbc.configuration.JdbcDynamicDataSourceAutoConfiguration;
import plus.easydo.starter.plugins.gen.controller.GenController;
import plus.easydo.starter.plugins.gen.controller.TemplateManagementController;
import plus.easydo.starter.plugins.gen.service.DataSourceDbService;
import plus.easydo.starter.plugins.gen.service.GenTableColumnService;
import plus.easydo.starter.plugins.gen.service.GenTableService;
import plus.easydo.starter.plugins.gen.service.GenerateService;
import plus.easydo.starter.plugins.gen.service.TemplateManagementService;
import plus.easydo.starter.plugins.gen.service.impl.DataSourceDbServiceImpl;
import plus.easydo.starter.plugins.gen.service.impl.GenTableColumnServiceImpl;
import plus.easydo.starter.plugins.gen.service.impl.GenTableServiceImpl;
import plus.easydo.starter.plugins.gen.service.impl.GenerateServiceImpl;
import plus.easydo.starter.plugins.gen.service.impl.TemplateManagementServiceImpl;
import plus.easydo.starter.plugins.gen.util.VelocityInitializer;

import javax.sql.DataSource;

/**
 * @author laoyu
 * @version 1.0
 */
@Configuration
@EnableConfigurationProperties(GenerateConfig.class)
@ConditionalOnBean(JdbcDynamicDataSourceAutoConfiguration.class)
@EnableJdbcDynamicDataSource
public class GenerateAutoConfigure {

    @Bean
    public GenTableService iGenTableService() {
        return new GenTableServiceImpl();
    }

    @Bean
    public GenTableColumnService iGenTableColumnService() {
        return new GenTableColumnServiceImpl();
    }

    @Bean
    public TemplateManagementService templateManagementService() {
        return new TemplateManagementServiceImpl();
    }

    @Bean
    public DataSourceDbService dataSourceDbService() {
        return new DataSourceDbServiceImpl();
    }

    @Bean
    public GenerateService generateService() {
        return new GenerateServiceImpl();
    }


    /**
     * 实例化 Velocity加载类
     * 将数据源注入到Velocity的DataSourceResourceLoader以支持数据源读取模板
     *
     * @param dataSource dataSource
     * @return plus.easydo.starter.plugins.gen.util.VelocityInitializer
     * @author laoyu
     */
    @Bean
    public VelocityInitializer velocityInitializer(DataSource dataSource) {
        DataSourceResourceLoader dataSourceResourceLoader = new DataSourceResourceLoader();
        dataSourceResourceLoader.setDataSource(dataSource);
        return new VelocityInitializer(dataSourceResourceLoader);
    }


    @Bean
    @ConditionalOnBean({GenTableService.class, GenTableColumnService.class})
    public GenController genController() {
        return new GenController();
    }

    @Bean
    @ConditionalOnBean(TemplateManagementService.class)
    public TemplateManagementController templateManagementController() {
        return new TemplateManagementController();
    }


}
